﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlogEngine.Core;
using System.Web.UI;
using _4mvcBlog.Core;
using _4mvcBlog.Core.ViewEngine;
using System.Web.UI.WebControls;

namespace _4mvcBlog.Controllers
{
    [Authorize]
    [ValidateInput(false)]
    [HandleError(Master="Admin")]
    public partial class AdminController : BaseController
    {
        public AdminController()
        {
            ViewData["PageTitle"] = "4mvcBlog 管理后台";
        }

        public ActionResult Index()
        {
            return RedirectToAction("Controls");
        }

        #region 评论管理
        /// <summary>
        /// 删除评论
        /// </summary>
        /// <param name="id">id的格式为：{postId},{commentId}</param>
        /// <returns></returns>
        [TestUserAuthorize]
        public ActionResult DelComment(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return base.ShowMsg("请指定要删除的评论的Id");
            }
            string[] ids = id.Split(',');
            if (ids.Length != 2)
            {
                return base.ShowMsg("要删除的评论Id的格式不正确");
            }
            try
            {
                Post post = Post.GetPost(new Guid(ids[0]));
                foreach (Comment comment in post.Comments)
                {
                    if (comment.Id == new Guid(ids[1]))
                    {
                        post.RemoveComment(comment);
                        return Redirect(Url.Action("Post", "Home", new { id = ids[0] }) + "#comments");
                    }
                }
                return base.ShowMsg("删除失败");
            }
            catch (Exception ex)
            {
                return base.ShowMsg(ex.Message);
            }
        }

        #endregion

        /// <summary>
        /// 将要用到的分类列表添加到ViewData中.
        /// </summary>
        /// <param name="categoryIds">选中的分类ID列表，用英文逗号分隔</param>
        private void InitCategorysViewData(string categoryIds)
        {
            AddCategoriesViewData();
            if (!string.IsNullOrEmpty(categoryIds))
            {
                ViewData["CategoryIds"] = categoryIds;
            }
        }

        #region 重写基类的方法

        [OutputCache(Location = OutputCacheLocation.None)]
        public override ActionResult Message()
        {
            return View("Message", "Admin", TempData["Messages"] as List<string>);
        }

        #region 修改Viewengine为AdminViewEngine

        //private static ViewEngineCollection _viewEngineCollection;
        ///// <summary>
        ///// 后台管理ViewEngine
        ///// </summary>
        //private static ViewEngineCollection ViewEngineCollection
        //{
        //    get
        //    {
        //        if (_viewEngineCollection == null)
        //        {
        //            _viewEngineCollection = new ViewEngineCollection();
        //            _viewEngineCollection.Add(new AdminViewEngine());
        //        }
        //        return _viewEngineCollection;
        //    }
        //}

        /// <summary>
        /// 重写view方法，修改IViewEngine为AdminViewEngine
        /// 这里可以修改以达到修改ViewEngine的目的，
        /// 但是Html.RenderPartial("viewName")还是使用ViewEngines.Engines来搜索View的，
        /// 所以如果在这里修改则在View中使用Html.RenderPartial("viewName")则会出错。
        /// </summary>
        /// <param name="viewName"></param>
        /// <param name="masterName"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        protected override ViewResult View(string viewName, string masterName, object model)
        {
            if (model != null)
            {
                base.ViewData.Model = model;
            }
            ViewResult result = new ViewResult();
            result.ViewName = viewName;
            result.MasterName = string.IsNullOrEmpty(masterName) ? "Admin" : masterName;
            result.ViewData = base.ViewData;
            result.TempData = base.TempData;
            //result.ViewEngineCollection = ViewEngineCollection;
            return result;
        }

        #endregion

        #endregion
    }
}
